<template>
  <el-form
    ref="formRef"
    v-loading="formLoading"
    :model="formData"
    :rules="formRules"
    label-width="120px"
  >
    <el-row v-for="(item, index) in formData.usersLinkmanSaveReqVOList" :key="index" :gutter="10">
      <el-col :span="7">
        <el-form-item label="姓名">
          <el-input v-model="item.username" placeholder="姓名" />
        </el-form-item>
      </el-col>
      <el-col :span="7">
        <el-form-item label="电话">
          <el-input v-model="item.mobile" maxlength="11" placeholder="请输入电话" />
        </el-form-item>
      </el-col>
      <el-col :span="7">
        <el-form-item label="关系">
          <el-select v-model="item.relation" placeholder="请选择">
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_RElATION)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="3" class="text-right">
        <el-button
          class="ml-5px!"
          type="primary"
          link
          :disabled="formData.usersLinkmanSaveReqVOList.length == 1"
          @click.prevent="removeTenantRelevancys(item)"
        >
          <Icon icon="ep:minus" :size="20" />
        </el-button>
        <el-button class="ml-5px!" type="primary" link @click.prevent="addEmergency">
          <Icon icon="ep:plus" :size="20" />
        </el-button>
      </el-col>
    </el-row>
  </el-form>
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

import * as UserApi from '@/api/system/user'

import { FormRules } from 'element-plus'
import { copyValueToTarget } from '@/utils'
import { propTypes } from '@/utils/propTypes'

defineOptions({ name: 'SystemPostForm' })
const props = defineProps({
  propFormData: {
    type: Object as PropType<UserApi.UserVO>,
    default: () => {}
  },
  activeName: propTypes.string.def('post')
})

const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const message = useMessage() // 消息弹窗

const formData = reactive({
  usersLinkmanSaveReqVOList: [
    {
      username: '',
      mobile: '',
      relation: ''
    }
  ] as any
})
const formRules = reactive<FormRules>({})
const formRef = ref() // 表单 Ref

/** 添加关联门店*/
const addEmergency = () => {
  formData.usersLinkmanSaveReqVOList.push({
    username: '',
    mobile: '',
    relation: ''
  })
}
/** 删除关联门店*/
const removeTenantRelevancys = (item) => {
  const index = formData.usersLinkmanSaveReqVOList.indexOf(item)
  if (index !== -1) {
    formData.usersLinkmanSaveReqVOList.splice(index, 1)
  }
}
/** 将传进来的值赋值给 formData */
watch(
  () => props.propFormData,
  (data) => {
    if (!data) {
      return
    }
    copyValueToTarget(formData, data)
  },
  {
    immediate: true
  }
)
/** 表单校验 */
const emit = defineEmits(['update:activeName'])
const validate = async () => {
  if (!formRef) return
  try {
    await unref(formRef)?.validate()
    // 校验通过更新数据
    Object.assign(props.propFormData, formData)
  } catch (e) {
    message.error('【紧急联系人信息】不完善，请填写相关信息')
    emit('update:activeName', 'emergency')
    throw e // 目的截断之后的校验
  }
}
defineExpose({ validate })
onMounted(async () => {})
</script>
